package com.cgtong.android.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cgtong.android.R;
import com.cgtong.android.adapter.MyParticipationListAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.ActivityBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.MyParticipationContract;
import com.cgtong.android.mvp.presenter.MyParticipationPresenter;
import com.cgtong.android.util.PostUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kingja.loadsir.callback.SuccessCallback;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Sky.Bing on 2017/10/19.
 */

public class MyParticipationActivity extends BaseTitleActivity implements MyParticipationContract.View {
    @BindView(R.id.refresh_layout)
    public SwipeRefreshLayout refreshLayout;
    @BindView(R.id.myparticipation_list)
    public RecyclerView myParticipationList;

    private int maxId;
    private MyParticipationListAdapter myParticipationListAdapter;
    private MyParticipationPresenter myParticipationPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_participation;
    }

    @Override
    protected String getContentTitle() {
        return getResources().getString(R.string.txt_me_partake);
    }

    @Override
    protected void loadDataByNet() {
        myParticipationPresenter = new MyParticipationPresenter(this);
        myParticipationListAdapter = new MyParticipationListAdapter();
        myParticipationList.setAdapter(myParticipationListAdapter);

        myParticipationList.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                maxId = 0;
                myParticipationPresenter.getMyParticipation(maxId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT);
            }
        });
        refreshLayout.setColorSchemeResources(R.color.colorAccent);
        /*myParticipationListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                int lastId = myParticipationListAdapter.getData().get(myParticipationListAdapter.getData().size() - 1).getId();
                myParticipationPresenter.loadMore(lastId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT);
            }
        }, myParticipationList);*/
        myParticipationListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                ActivitiesDetailActivity.actionStart(MyParticipationActivity.this, myParticipationListAdapter.getData().get(i).getId());
            }
        });
        myParticipationPresenter.getMyParticipation(maxId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT);
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        refreshLayout.setRefreshing(isRefresh);
    }

    @Override
    public void setEnableLoadMore(boolean enabled) {
        myParticipationListAdapter.setEnableLoadMore(enabled);
    }

    @Override
    public void showParticipationActivityList(final boolean isRefresh, final List<ActivityBean> activityBeanList) {
        final int size = activityBeanList == null ? 0 : activityBeanList.size();
        if (size > 0) {
            if (isRefresh) {
                myParticipationListAdapter.setNewData(activityBeanList);
            } else {
                if (size > 0) {
                    myParticipationList.post(new Runnable() {
                        @Override
                        public void run() {
                            myParticipationListAdapter.addData(activityBeanList);
                        }
                    });
                }
            }
            myParticipationList.post(new Runnable() {
                @Override
                public void run() {
                    if (size < ConstantManager.HOME_MAX_NEWS) {
                        myParticipationListAdapter.loadMoreEnd(isRefresh);
                    } else {
                        myParticipationListAdapter.loadMoreComplete();
                    }
                }
            });
        } else {
            myParticipationListAdapter.setEmptyView(R.layout.layout_empty, (ViewGroup) myParticipationList.getParent());
        }
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
    }

    @Override
    public void getMyParticipationActivityError(ApiException e) {
        if (e.getCode() == -1) {
            Intent intent = new Intent(MyParticipationActivity.this, LoginActivity.class);
            startActivityForResult(intent, ConstantManager.REQUEST_LOGIN_CODE);
        } else if(e.getCode() == -1001) {
            setRefreshing(false);
            myParticipationListAdapter.setEmptyView(R.layout.layout_empty, (ViewGroup) myParticipationList.getParent());
        } else {
            setRefreshing(false);
            myParticipationListAdapter.setEmptyView(R.layout.layout_error, (ViewGroup) myParticipationList.getParent());
        }
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ConstantManager.REQUEST_LOGIN_CODE) {
            if (resultCode == RESULT_OK) {
                myParticipationPresenter.getMyParticipation(maxId, ConstantManager.MAX_WONDERFUL_NEWS_COUNT);
            } else {
                finish();
            }
        }
    }
}